<template>
  <div class="container-content">
    <el-form
      ref="formRef"
      :model="formObject"
      label-width="100px"
      :rules="rules"
      status-icon
      label-position="left"
    >
      <el-dropdown
        style="position: absolute; right: 50px; top: 20px; z-index: 10"
        trigger="click"
        @command="handleCommand"
      >
        <el-button size="small" type="primary">
          选单<i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="salesContract">销售合同</el-dropdown-item>
          <el-dropdown-item command="saleOrder">销售订单</el-dropdown-item>
          <el-dropdown-item command="2">销售报价单</el-dropdown-item>
          <el-dropdown-item command="3">物料清单</el-dropdown-item>
          <el-dropdown-item command="4">采购订单</el-dropdown-item>
          <el-dropdown-item command="5">网上订单</el-dropdown-item>
          <el-dropdown-item command="6">预测单</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-collapse v-model="activeName" @change="handleClick">
        <el-collapse-item title="基本" name="basic">
          <el-row :gutter="15">
            <el-col :span="8">
              <el-form-item label="业务类型" prop="docBusinessType">
                <el-select
                  v-model="formObject.docBusinessType"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in documentTypeList"
                    :key="item.keyCode"
                    :label="item.title"
                    :value="item.keyCode"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="单据编码" prop="docCode">
                <el-input
                  v-model="formObject.docCode"
                  placeholder="保存时自动生成"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="报价日期" prop="askTime">
                <el-date-picker
                  v-model="formObject.askTime"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  :style="{ width: '100%' }"
                  placeholder="请输入日期"
                  clearable
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="询价单号" prop="askCode">
                <el-input
                  v-model="formObject.askCode"
                  placeholder=""
                  clearable
                  disabled
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="比价模式" prop="compareMode">
                <el-select
                  v-model="formObject.compareMode"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in compareModeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="供应商" prop="supplierName">
                <el-input
                  v-model="formObject.supplierName"
                  placeholder="请选择供应商"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('supplier')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="联系人" prop="contracts">
                <el-input
                  v-model="formObject.contracts"
                  placeholder="请输入联系人"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="电话" prop="phone">
                <el-input
                  v-model="formObject.phone"
                  placeholder="请输入电话"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="邮箱" prop="email">
                <el-input
                  v-model="formObject.email"
                  placeholder="请输入邮箱"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label-width="120px"
                label="报价截止日期"
                prop="quotationDeadline"
              >
                <el-date-picker
                  v-model="formObject.quotationDeadline"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  :style="{ width: '100%' }"
                  placeholder="请输入日期"
                  clearable
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label-width="120px"
                label="价格生效日期"
                prop="priceEffectiveDate"
              >
                <el-date-picker
                  v-model="formObject.priceEffectiveDate"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  :style="{ width: '100%' }"
                  placeholder="请输入日期"
                  clearable
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label-width="120px"
                label="价格失效日期"
                prop="priceExpirationDate"
              >
                <el-date-picker
                  v-model="formObject.priceExpirationDate"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  :style="{ width: '100%' }"
                  placeholder="请输入日期"
                  clearable
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="单据状态" prop="docStatus">
                <el-select
                  v-model="formObject.docStatus"
                  placeholder=""
                  style="width: 100%"
                  disabled
                >
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="结算币别" prop="settlementCurrencyName">
                <el-input
                  v-model="formObject.settlementCurrencyName"
                  placeholder="请输入结算币别"
                  clearable
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('currency')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="付款条件" prop="payConditionName">
                <el-input
                  v-model="formObject.payConditionName"
                  placeholder="请输入付款条件"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('payCondition')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="采购组织" prop="purchaseOrg">
                <el-input
                  v-model="formObject.purchaseOrg"
                  placeholder=""
                  clearable
                  disabled
                  :style="{ width: '100%' }"
                >
                  <!-- <i
                                        slot="suffix"
                                        class="el-input__icon el-icon-notebook-2"
                                        style="cursor: pointer"
                                        @click="addDialog('saleDept')"
                                    ></i> -->
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="本位币" prop="baseCurrencyName">
                <el-input
                  v-model="formObject.baseCurrencyName"
                  placeholder="请输入本位币"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('baseCurrency')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="结算方式" prop="settlementMethodName">
                <el-input v-model="formObject.settlementMethodName">
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('settlementType')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="采购员" prop="purchaseByName">
                <el-input
                  v-model="formObject.purchaseByName"
                  placeholder="请选择采购员"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('saleId')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="汇率类型" prop="exchangeRateTypeName">
                <el-input
                  v-model="formObject.exchangeRateTypeName"
                  placeholder="请选择汇率类型"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-notebook-2"
                    style="cursor: pointer"
                    @click="addDialog('rateType')"
                  ></i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="价格类型" prop="priceType">
                <el-select
                  v-model="formObject.priceType"
                  placeholder="请选择"
                  style="width: 100%"
                  disabled
                >
                  <el-option
                    v-for="item in priceTypeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="汇率">
                <el-input v-model="formObject.exchangeRate" clearable disabled>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="是否含税" prop="isTax">
                <el-radio v-model="formObject.isTax" :label="false" border
                  >否</el-radio
                >
                <el-radio v-model="formObject.isTax" :label="true" border
                  >是</el-radio
                >
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label-width="110px"
                label="是否价外税"
                prop="isExtraPriceTax"
              >
                <el-radio
                  v-model="formObject.isExtraPriceTax"
                  :label="false"
                  border
                  >否</el-radio
                >
                <el-radio
                  v-model="formObject.isExtraPriceTax"
                  :label="true"
                  border
                  >是</el-radio
                >
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item
                label-width="120px"
                label="是否内部单据"
                prop="isInternal"
              >
                <el-radio v-model="formObject.isInternal" :label="false" border
                  >否</el-radio
                >
                <el-radio v-model="formObject.isInternal" :label="true" border
                  >是</el-radio
                >
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input
                  v-model="formObject.remark"
                  placeholder="请输入备注"
                  clearable
                  type="textarea"
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>

        <el-collapse-item title="明细信息" name="detail">
          <el-button
            type="text"
            size="medium"
            @click="addNewRow('inquiryDetailList')"
            >新增行</el-button
          >
          <el-table
            :data="formObject.inquiryDetailList"
            style="width: 100%"
            highlight-current-row
            border
          >
            <el-table-column label="操作" width="60">
              <template slot-scope="scope">
                <el-button
                  plain
                  icon="el-icon-minus"
                  size="mini"
                  @click="
                    handleDataDelete(
                      scope.$index,
                      scope.row,
                      'inquiryDetailList'
                    )
                  "
                ></el-button>
              </template>
            </el-table-column>
            <el-table-column
              type="index"
              label="序号"
              width="55"
              align="center"
            ></el-table-column>
            <el-table-column width="200" label="" prop="materialCode">
              <template slot="header">
                物料编码<span class="required">*</span>
              </template>
              <template slot-scope="scope">
                <el-form-item
                  label-width="0px"
                  style="margin: 0"
                  :rules="rules.materialCode"
                  :prop="'inquiryDetailList.' + scope.$index + '.materialCode'"
                >
                  <el-input v-model="scope.row.materialCode" clearable>
                    <i
                      slot="suffix"
                      @click="addDialog('material', scope.$index)"
                      class="el-input__icon el-icon-notebook-2"
                      style="cursor: pointer"
                    ></i>
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="物料名称" prop="materialName">
              <template slot-scope="scope">
                <el-form-item label-width="0px" style="margin: 0">
                  <el-input
                    v-model="scope.row.materialName"
                    clearable
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="规格型号" prop="materialModel">
              <template slot-scope="scope">
                <el-form-item
                  prop="materialModel"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input
                    v-model="scope.row.materialModel"
                    disabled
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="计价单位" prop="unitName">
              <template slot-scope="scope">
                <el-form-item
                  label-width="0px"
                  style="margin: 0"
                  :rules="rules.unit"
                  :prop="'inquiryDetailList.' + scope.$index + '.unitName'"
                >
                  <el-input v-model="scope.row.unitName">
                    <i
                      slot="suffix"
                      @click="addDialog('unit', scope.$index)"
                      class="el-input__icon el-icon-notebook-2"
                      style="cursor: pointer"
                    ></i
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="询价数量" prop="num">
              <template slot-scope="scope">
                <el-form-item prop="num" label-width="0px" style="margin: 0">
                  <el-input v-model="scope.row.num"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="报价数量" prop="quotationNum">
              <template slot-scope="scope">
                <el-form-item
                  prop="quotationNum"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.quotationNum"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="单价" prop="price">
              <template slot-scope="scope">
                <el-form-item prop="price" label-width="0px" style="margin: 0">
                  <el-input v-model="scope.row.price"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="含税单价" prop="taxPrice">
              <template slot-scope="scope">
                <el-form-item
                  prop="taxPrice"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.taxPrice"> </el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="税率%" prop="taxRate">
              <template slot-scope="scope">
                <el-form-item
                  prop="taxRate"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input v-model="scope.row.taxRate">
                    <i
                      slot="suffix"
                      class="el-input__icon el-icon-notebook-2"
                      style="cursor: pointer"
                      @click="addDialog('taxRate', scope.$index)"
                    ></i>
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="参考币别" prop="referenceCurrency">
              <template slot-scope="scope">
                <el-form-item
                  prop="referenceCurrency"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input
                    v-model="scope.row.referenceCurrency"
                    clearable
                    disabled
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="参考价" prop="referencePrice">
              <template slot-scope="scope">
                <el-form-item
                  prop="referencePrice"
                  label-width="0px"
                  style="margin: 0"
                >
                  <el-input
                    v-model="scope.row.referencePrice"
                    clearable
                    disabled
                  ></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="备注" prop="remark">
              <template slot-scope="scope">
                <el-form-item prop="remark" label-width="0px" style="margin: 0">
                  <el-input v-model="scope.row.remark" clearable></el-input>
                </el-form-item>
              </template>
            </el-table-column>
          </el-table>
        </el-collapse-item>
      </el-collapse>
    </el-form>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialog"
      append-to-body
      width="70%"
      destroy-on-close
      v-if="dialog"
    >
      <component
        :is="currentCom"
        :id="rowId"
        :visible.sync="dialog"
        @getSelectVal="getSelectVal"
      ></component>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelSelect">取 消</el-button>
        <el-button type="primary" @click="confirmSelect">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { listDept } from "@/api/system/dept";
import { getAreaList } from "@/api/area/area";
import {
  getOrderDetail,
  addList,
  getDocTypeList,
  addInquiryList,
} from "@/api/system/purchase";
import { addQuotationList } from "@/api/system/sale";
import { getCompute } from "@/api/finance/info";

import SaleId from "@/components/dialog/saleId";
import Customer from "@/components/dialog/customer";
import BusinessGroup from "@/components/dialog/businessGroup";
import QueryMaterialList from "@/components/dialog/queryMaterialList";
import PriceUnit from "@/components/dialog/priceUnit";
import TaxRate from "@/components/dialog/taxRate";

import Currency from "@/components/dialog/currency";
import paymentTerms from "@/components/dialog/paymentTerms";
import Dept from "@/components/dialog/dept";
import settlementType from "@/components/dialog/settlementType";
import rateType from "@/components/dialog/rateType";
import supplier from "@/components/dialog/supplier";

export default {
  props: ["rowId"],
  components: {
    SaleId,
    Customer,
    Dept,
    BusinessGroup,
    QueryMaterialList,
    PriceUnit,
    TaxRate,
    Currency,
    paymentTerms,
    settlementType,
    rateType,
    supplier,
  },
  data() {
    return {
      activeName: ["basic", "detail"],
      dialog: false,
      counties: [],
      orgData: [], //创建组织弹窗列表
      orgSelect: [], //选中项
      selectItem: [],
      orgTotal: "", //创建组织弹窗分页
      dialogType: "",
      dialogIndex: "",
      currentCom: "", //弹窗组件
      dialogTitle: "", //弹窗标题
      props: {},
      formObject: {
        askCode: "",
        askTime: "",
        baseCurrency: "",
        closeBy: "",
        closeStatus: "",
        closeTime: "",
        compareMode: "",
        confirmBy: "",
        confirmStatus: "",
        contracts: "",
        createBy: "",
        createTime: "",
        docBusinessType: "",
        docCode: "",
        docStatus: "",
        docType: "",
        email: "",
        examineBy: "",
        examineTime: "",
        exchangeRate: "",
        exchangeRateType: "",
        id: "",
        inquiryName: "",
        isAll: true,
        isAllowAdoptingSuppliers: true,
        isExtraPriceTax: true,
        isInternal: true,
        isTax: true,
        isUsed: "",
        payCondition: "",
        phone: "",
        priceEffectiveDate: "",
        priceExpirationDate: "",
        priceType: "1",
        purchaseBy: "",
        purchaseDept: "",
        purchaseGroup: "",
        purchaseOrg: "",
        quotationDeadline: "",
        remark: "",
        settlementCurrency: "",
        settlementCurrencyName: "",
        settlementMethod: "",
        supplierCode: "",
        thirdMessage: true,
        thirdTemplate: "",
        type: "1",
        updateBy: "",
        updateTime: "",
        businessType: "35",
        modelId: this.$store.state.workflow.businessConfig.id,
        inquiryDetailList: [],
      },
      rules: {
        docBusinessType: [
          {
            required: true,
            message: "请选择业务类型",
            trigger: "change",
          },
        ],
        askTime: [
          {
            required: true,
            message: "请选择报价日期",
            trigger: "change",
          },
        ],
        priceExpirationDate: [
          {
            required: true,
            message: "请选择价格失效日期",
            trigger: "change",
          },
        ],
        quotationDeadline: [
          {
            required: true,
            message: "请选择报价截止日期",
            trigger: "change",
          },
        ],
        priceEffectiveDate: [
          {
            required: true,
            message: "请选择价格生效日期",
            trigger: "change",
          },
        ],
        settlementCurrencyName: [
          {
            required: true,
            message: "请选择结算币别",
            trigger: "blur",
          },
        ],
        supplierName: [
          {
            required: true,
            message: "请选择供应商",
            trigger: "blur",
          },
        ],
        materialCode: [
          {
            required: true,
            message: "请选择物料编码",
            trigger: "blur",
          },
        ],
      },
      documentTypeList: [],
      compareModeList: [],
      priceTypeList: [
        {
          label: "采购",
          value: "1",
        },
        {
          label: "委外",
          value: "2",
        },
        {
          label: "VMI",
          value: "3",
        },
        {
          label: "工序委外",
          value: "4",
        },
        {
          label: "工序协作",
          value: "5",
        },
      ],
    };
  },
  methods: {
    // 页签切换事件
    handleClick(tab, event) {},
    // 附件上传
    handleFileChange() {
      console.log("文件上传");
    },
    // 单据类型选择change事件
    docTypeChange(value) {
      if (value) {
        this.formObject.docBusinessType = value;
      }
    },
    // 基础表单提交
    submit() {
      this.$refs["formRef"].validate((valid) => {
        if (!valid) return;
        // TODO 提交表单
        addInquiryList(this.formObject).then((res) => {
          if (res.code === 200) {
            this.$message({
              message: "新增成功！",
              type: "success",
            });
            this.$store.commit("workflow/SET_BUSINESSDRAWER", false);
          }
        });
      });
    },

    // 基础表单取消
    formRest(formRef) {
      this.$refs[formRef].resetFields();
    },
    // 删除行
    handleDataDelete(index, row, type) {
      this.$confirm("请确认是否要删除此行？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          if (type === "inquiryDetailList") {
            // 明细信息
            this.formObject.inquiryDetailList.splice(index, 1);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 物料明细 添加行
    addInquiryDetailList(index, row) {
      // 物料明细 obj
      let detailObj = this.getDetialObj();
      this.formObject.inquiryDetailList.push(detailObj);
    },

    //添加新行
    addNewRow(type) {
      if (type === "inquiryDetailList") {
        this.addInquiryDetailList(); //明细信息
      }
    },
    // 处理弹窗弹出
    handleOpen() {
      const nameMap = {
        customer: () => {
          this.dialogTitle = "选择客户";
          this.currentCom = Customer;
        },
        dept: () => {
          this.dialogTitle = "选择部门";
          this.currentCom = Dept;
        },

        saleId: () => {
          this.dialogTitle = "选择采购员";
          this.currentCom = SaleId;
        },
        deptGroup: () => {
          this.dialogTitle = "选择采购组";
          this.currentCom = BusinessGroup;
        },
        material: () => {
          this.dialogTitle = "选择物料";
          this.currentCom = QueryMaterialList;
        },
        unit: () => {
          this.dialogTitle = "选择单位";
          this.currentCom = PriceUnit;
        },
        priceUnit: () => {
          this.dialogTitle = "选择单位";
          this.currentCom = PriceUnit;
        },
        taxRate: () => {
          this.dialogTitle = "选择税率";
          this.currentCom = TaxRate;
        },
        currency: () => {
          this.dialogTitle = "选择币别";
          this.currentCom = Currency;
        },
        baseCurrency: () => {
          this.dialogTitle = "选择本位币";
          this.currentCom = Currency;
        },
        payCondition: () => {
          this.dialogTitle = "选择付款条件";
          this.currentCom = paymentTerms;
        },
        settlementType: () => {
          this.dialogTitle = "选择结算方式";
          this.currentCom = settlementType;
        },
        rateType: () => {
          this.dialogTitle = "选择汇率类型";
          this.currentCom = rateType;
        },
        supplier: () => {
          this.dialogTitle = "选择供应商";
          this.currentCom = supplier;
        },
      };
      nameMap[this.dialogType] ? nameMap[this.dialogType]() : "";
    },
    // 选择弹窗 item
    getSelectVal(val) {
      this.selectItem = val;
    },
    //点击选择dialog中的一条数据回显
    addDialog(type, index) {
      this.dialog = true;
      this.dialogType = type;
      this.dialogIndex = index;
      this.handleOpen();
    },

    // 创建组织
    selectOrg() {
      this.showOrg = true;
      let level = 1;
      listDept({ level }).then((res) => {
        if (res.code === 200) {
          this.orgData = res.data;
        }
        return;
      });
    },
    // 创建组织item
    selectOrgItem(val) {
      if (val.length === 1) {
        this.orgSelect = val;
        this.showOrg = false;
      }
    },
    //获取单据类型
    getDocTypeListApi() {
      getDocTypeList({ docCode: "SCP_QuoteBill" }).then((res) => {
        if (res.code === 200) {
          this.documentTypeList = res.rows;
        }
      });
    },
    // 获取比价模式字典
    getCompareModeList() {
      // 比价模式
      this.getDicts("sys_compare_mode").then((res) => {
        if (res.code === 200) {
          this.compareModeList = res.data.map((item) => {
            return {
              value: item.dictCode,
              label: item.dictLabel,
            };
          });
        }
      });
      // 交货方式
      this.getDicts("delivery_method").then((res) => {
        if (res.code === 200) {
          this.deliveryMethodList = res.data.map((item) => {
            return {
              value: item.dictCode,
              label: item.dictLabel,
            };
          });
        }
      });
    },
    //下拉类型选择
    handleCommand(val) {
      this.dialogType = val;
      this.dialog = true;
      this.handleOpen();
    },

    cancelSelect() {
      this.dialog = false;
      this.selectItem = [];
    },
    confirmSelect() {
      const nameMap = {
        customer: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.formObject.customerId = this.selectItem[0].id;
          this.formObject.customerName = this.selectItem[0].customerName;
          this.dialog = false;
        },
        supplier: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.formObject.supplierCode = this.selectItem[0].supplierCode;
          this.$set(this.formObject, "supplierId", this.selectItem[0].id);
          this.$set(
            this.formObject,
            "supplierName",
            this.selectItem[0].supplierName
          );
          this.dialog = false;
        },
        dept: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.formObject.purchaseDept = this.selectItem[0].deptId;
          this.$set(
            this.formObject,
            "purchaseDeptName",
            this.selectItem[0].deptName
          );
          this.$set(
            this.formObject,
            "purchaseDeptCode",
            this.selectItem[0].deptCode
          );
          this.dialog = false;
        },
        saleId: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.formObject.purchaseBy = this.selectItem[0].userId;
          this.$set(
            this.formObject,
            "purchaseByName",
            this.selectItem[0].userName
          );
          this.dialog = false;
        },
        deptGroup: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.formObject.purchaseGroup = this.selectItem[0].id;
          this.$set(
            this.formObject,
            "purchaseGroupName",
            this.selectItem[0].businessGroupType
          );
          this.dialog = false;
        },
        material: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条数据！");
            return;
          }
          this.selectItem.forEach((item) => {
            let detailObj = this.getDetialObj();
            Object.keys(item).forEach((itemKey) => {
              Object.keys(detailObj).forEach((detailKey) => {
                if (itemKey === detailKey) {
                  detailObj[detailKey] = item[itemKey];
                }
                if (itemKey === "id") {
                  detailObj["materialId"] = item[itemKey];
                }
                if (itemKey === "model") {
                  detailObj["materialModel"] = item[itemKey];
                }
                if (itemKey === "purchaseUnitName") {
                  detailObj["unitName"] = item[itemKey];
                }
                if (itemKey === "purchaseUnit") {
                  detailObj["unit"] = item[itemKey];
                }
              });
            });
            this.$set(
              this.formObject.inquiryDetailList,
              this.dialogIndex,
              detailObj
            );
          });
          this.dialog = false;
        },
        unit: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条记录！");
            return;
          }
          // 询价单位 unit
          this.formObject.inquiryDetailList[this.dialogIndex].unit =
            this.selectItem[0].id;
          this.$set(
            this.formObject.inquiryDetailList[this.dialogIndex],
            "unitName",
            this.selectItem[0].meteringUnitName
          );
          this.$set(
            this.formObject.inquiryDetailList[this.dialogIndex],
            "unitCode",
            this.selectItem[0].meteringUnitCode
          );

          this.dialog = false;
        },
        priceUnit: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条记录！");
            return;
          }
          // 计价单位
          this.formObject.materialDetailEntities[this.dialogIndex].pricingUnit =
            this.selectItem[0].meteringUnitCode;
          this.formObject.materialDetailEntities[
            this.dialogIndex
          ].pricingUnitName = this.selectItem[0].meteringUnitName;
          this.dialog = false;
        },
        taxRate: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条记录！");
            return;
          }
          // 税率
          this.formObject.inquiryDetailList[this.dialogIndex].tax =
            this.selectItem[0].id;
          this.$set(
            this.formObject.inquiryDetailList[this.dialogIndex],
            "taxCode",
            this.selectItem[0].rateCode
          );
          this.$set(
            this.formObject.inquiryDetailList[this.dialogIndex],
            "taxRate",
            this.selectItem[0].rate
          );

          this.dialog = false;
        },
        rateType: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条记录！");
            return;
          }
          this.formObject.exchangeRateType = this.selectItem[0].id;
          this.$set(
            this.formObject,
            "exchangeRateTypeName",
            this.selectItem[0].name
          );
          this.$set(
            this.formObject,
            "exchangeRateTypeCode",
            this.selectItem[0].code
          );
        },
        currency: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条记录！");
            return;
          }
          this.formObject.settlementCurrency = this.selectItem[0].id;
          this.$set(
            this.formObject,
            "settlementCurrencyName",
            this.selectItem[0].currencyName
          );
          this.formObject.exchangeRate = this.selectItem[0].rate;
          this.dialog = false;
        },
        baseCurrency: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条记录！");
            return;
          }
          this.formObject.baseCurrency = this.selectItem[0].id;
          this.$set(
            this.formObject,
            "baseCurrencyName",
            this.selectItem[0].currencyName
          );
          this.dialog = false;
        },
        settlementType: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条记录！");
            return;
          }
          this.formObject.settlementMethod = this.selectItem[0].id;
          this.$set(
            this.formObject,
            "settlementMethodName",
            this.selectItem[0].title
          );
          this.$set(
            this.formObject,
            "settlementMethodCode",
            this.selectItem[0].bizCode
          );
          this.dialog = false;
        },
        payCondition: () => {
          if (this.selectItem.length !== 1) {
            this.$message.error("请选择一条记录！");
            return;
          }
          this.formObject.payCondition = this.selectItem[0].id;
          this.$set(
            this.formObject,
            "payConditionName",
            this.selectItem[0].title
          );
          this.dialog = false;
        },
      };
      nameMap[this.dialogType] ? nameMap[this.dialogType]() : "";
    },
    getDetialObj() {
      return {
        arrivalTime: "",
        deliveryMethod: "",
        id: "",
        inquiryDetailEntry: {
          arrivalTime: "",
          auxiliaryProperties: "",
          compareRelatedNum: 0,
          confirmComment: "",
          confirmNum: 0,
          confirmPrice: "",
          confirmSupplier: "",
          confirmTaxPrice: "",
          containTaxPrice: "",
          deliveryAddress: "",
          deliveryMethod: "",
          detailId: "",
          id: "",
          inquiryId: "",
          inquiryNum: 0,
          inquiryUnit: "",
          materialCode: "",
          materialModel: "",
          money: "",
          orderRelatedNum: 0,
          price: "",
          quotationNum: 0,
          remark: "",
          requireDept: "",
          requireOrg: "",
          result: "",
          sourceDocCode: "",
          sourceDocType: "",
          tax: "",
          taxMoney: "",
          taxPrice: "",
        },
        inquiryId: "",
        isUsed: "",
        materialCode: "",
        materialId: "",
        materialModel: "",
        materialName: "",
        num: 0,
        price: "",
        quotationNum: 0,
        referenceCurrency: "",
        referencePrice: "",
        remark: "",
        tax: "",
        taxPrice: "",
        type: "1",
        unit: "",
      };
    },
    // 计算值
    caculateNum(row, index) {
      // 数量，税率，价税合计，汇率
      console.log("触发", row);
      if (row == undefined) {
        return;
      }
      if (
        row.number &&
        row.taxRate &&
        row.totalTaxPrice &&
        this.formObject.rate
      ) {
        let totalTaxAmount = row.totalTaxPrice;
        let number = row.number;
        let taxRate = row.taxRate;
        let rate = this.formObject.rate;
        getCompute({ number, taxRate, totalTaxAmount, rate }).then((res) => {
          this.$set(
            this.formObject.materialDetailEntities[index],
            "price",
            res.data.price
          );
          this.$set(
            this.formObject.materialDetailEntities[index],
            "amountMoney",
            res.data.amount
          );

          this.$set(
            this.formObject.materialDetailEntities[index],
            "taxAmount",
            res.data.taxAmount
          );

          this.$set(
            this.formObject.materialDetailEntities[index],
            "taxPrice",
            res.data.taxUnitPrice
          );
        });
      }
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "总计";
          return;
        }
        if (
          column.property === "number" ||
          column.property === "pricingNum" ||
          column.property === "amountMoney" ||
          column.property === "taxAmount" ||
          column.property === "totalTaxPrice"
        ) {
          const values = data.map((item) => Number(item[column.property]));
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] += "";
          } else {
            sums[index] = "N/A";
          }
        } else {
          return;
        }
      });

      return sums;
    },
  },
  created() {
    this.getDocTypeListApi();
    this.getCompareModeList();
  },
};
</script>
<style lang="scss" scoped>
// .add {
//   padding: 15px;
// }
.panel-title {
  font-size: 18px;
  color: #37474f;
  font-weight: 500;
  text-shadow: rgba(0, 0, 0, 0.15) 0 0 1px;
}

::v-deep .el-form-item__content {
  margin-left: 0;
}

.main-responsible {
  font-size: 14px;
  font-weight: 550;
  color: #263238;
  position: relative;
  text-indent: 15px;
}
::v-deep .el-table td.el-table__cell div {
  overflow: visible;
}

::v-deep .el-table td.el-table__cell {
  padding-bottom: 20px;
}
</style>
